package com.linsen.erp.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linsen.erp.admin.entity.RoleEntity;
import com.linsen.erp.admin.provider.dto.RoleDTO;
import com.linsen.erp.admin.provider.qo.RoleQo;
import com.linsen.erp.admin.provider.vo.RoleVO;
import com.linsen.erp.admin.service.IRoleService;
import com.linsen.core.common.enums.DeleteType;
import com.linsen.core.common.util.ConvertUtil;
import com.linsen.core.common.util.Result;
import com.linsen.core.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author zhaoyong
 * @since 2023-07-24
 */
@RestController
@RequestMapping("/admin/role")
@Api(tags = "角色管理")
@RequiredArgsConstructor
public class RoleController {
    private final IRoleService iRoleService;

    //@SaCheckPermission("admin:role:page")
    @PostMapping("/page")
    @ApiOperation(value = "分页查询",response = RoleVO.class)
    public Result page(@RequestBody RoleQo roleQo) {
        return Result.data(this.iRoleService.page(roleQo));
    }

    @GetMapping("/list")
    @ApiOperation(value = "角色列表",response = RoleVO[].class)
    public Result list() {
        List<RoleEntity> roleEntityList = this.iRoleService.list(
                new LambdaQueryWrapper<RoleEntity>()
                        .eq(RoleEntity::getIsDeleted, DeleteType.NORMAL.getValue())
        );
        return Result.data(ConvertUtil.convert(roleEntityList,RoleVO.class));
    }

    @GetMapping("info")
    @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "string")
    @ApiOperation(value = "角色详情",response = RoleVO.class)
    public Result get(@ApiIgnore @RequestParam(value = "id") String id) {
        return Result.data(this.iRoleService.get(id));
    }

    //@SaCheckPermission("admin:role:save")
    @Log(value = "新增角色")
    @PostMapping("save")
    @ApiOperation("新增角色")
    public Result save(@RequestBody RoleDTO roleDTO) {
        iRoleService.saveRole(roleDTO);

        return Result.ok();
    }

    //@SaCheckPermission("admin:role:update")
    @Log(value = "修改角色")
    @PostMapping("update")
    @ApiOperation("修改角色")
    public Result update(@RequestBody RoleDTO roleDTO) {
        iRoleService.updateRole(roleDTO);

        return Result.ok();
    }

    //@SaCheckPermission("admin:role:delete")
    @Log(value = "删除角色")
    @PostMapping("delete")
    @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "string")
    @ApiOperation("删除角色")
    public Result delete(@ApiIgnore @RequestParam("id") String id) {
        iRoleService.delete(id);

        return Result.ok();
    }
}
